package thread;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-12
 * Time：18:59
 */
public class Demo14 {
    static class Counter {
        public int flag = 0;
    }

    public static void main(String[] args) {
        Counter counter = new Counter();
        //创建一个t1线程，如果flag的值为0，就一直循环运行下去
        Thread t1 = new Thread(() -> {
            while (counter.flag==0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("循环结束！");
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            counter.flag = 1;
            System.out.println("已将Counter.flag的值改为1～");
        });

        t1.start();
        t2.start();
    }
}
